﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DTU_Test;

/// <summary>
/// 透传数据帧
/// </summary>
public class DTU_Protocol_Frame
{
    public static readonly byte[] STARTCODE = { 0xAA, 0X55 }; //起始符
    public static readonly byte[] ENDCODE = { 0X0D, 0X0A }; //结束符

    /// <summary>
    /// //命令码
    /// </summary>
    public byte CommandCode { get; set; }

    /// <summary>
    /// //数据长度,2个字节
    /// </summary>
    public ushort DataLength { get; set; }

    /// <summary>
    /// 数据
    /// </summary>
    public byte[]? Data { get; set; }

    /// <summary>
    /// XOR校验和，指的是数据区的校验和，起始符和结束符不参与校验
    /// </summary>
    public byte XORCRC { get; set; }

    /// <summary>
    /// 名称
    /// </summary>
    public string? Name { get; set; }

    /// <summary>
    /// 转换成字节
    /// </summary>
    /// <returns></returns>
    public byte[] ToBytes()
    {
        List<byte> bytes = new List<byte>();
        bytes.AddRange(STARTCODE);
        bytes.Add(this.CommandCode);
        bytes.AddRange(this.DataLength.ToBytes());
        if (this.Data != null)
            bytes.AddRange(this.Data);
        bytes.Add(XORCRC);
        bytes.AddRange(ENDCODE);
        return bytes.ToArray();
    }

    /// <summary>
    /// 构建读设备信息帧
    /// </summary>
    /// <returns></returns>
    public static DTU_Protocol_Frame BuildReadDeviceInfoFrame()
    {
        DTU_Protocol_Frame frame = new DTU_Protocol_Frame();
        frame.CommandCode = 0x07;
        frame.DataLength = 0;
        frame.XORCRC = 0x00;
        return frame;
    }

    /// <summary>
    /// 构建写设备信息
    /// </summary>
    /// <param name="setting"></param>
    /// <returns></returns>
    public static DTU_Protocol_Frame BuildWriteDeviceInfoFrame(DTUSetting setting)
    {
        DTU_Protocol_Frame frame = new DTU_Protocol_Frame();
        frame.CommandCode = 0x05;
        frame.Data = setting.ToBytes();
        frame.DataLength = (ushort)frame.Data.Length;
        byte check = 0;
        for (int i = 0; i < frame.Data.Length; i++)
        {
            check ^= frame.Data[i];
        }
        frame.XORCRC = check; //校验和
        return frame;
    }

    /// <summary>
    /// 心跳包收到回复
    /// </summary>
    /// <returns></returns>
    public static DTU_Protocol_Frame BuildHeatBeatRespFrame()
    {
        DTU_Protocol_Frame frame = new DTU_Protocol_Frame();
        frame.CommandCode = 0x04;
        frame.DataLength = 0;
        frame.XORCRC = 0x00;
        return frame;
    }

    public static DTUSetting DecodeDTUSetting(byte[] data )
    {
        if(data !=null )
        {
            if (data[0]==0xAA && data[1]==0x55 && data[data.Length-2]==0x0D && data[data.Length-1]==0x0A)
            {
                //去掉起始符和结束符
                byte[] settingData = new byte[data.Length - 8];
                Array.Copy(data, 5, settingData, 0, settingData.Length);
                string json = ASCIIEncoding.Default.GetString(settingData);
                return Helper.FromJson<DTUSetting>(json );
            }
        }
        return null;

    }
}